package com.study.jyg.rxjava2study.utils;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

import androidx.appcompat.app.AppCompatActivity;

import com.study.jyg.rxjava2study.BuildConfig;
import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.utils.dialog.DialogUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;


public class RxPermissionUtil {


    @SuppressLint("CheckResult")
    public static void initPermission(AppCompatActivity compatActivity, int requestCode, RxPermissionListener listener, String... permissions) {
        boolean[] success = new boolean[permissions.length];
        for (int i = 0; i < success.length; i++) {
            success[i] = false;
        }
        new RxPermissions(compatActivity)
                .requestEach(permissions)
                .subscribe(permission -> {
                    if (permission.granted) {// 用户已经同意该权限
                        for (int i = 0; i < permissions.length; i++) {
                            if (permissions[i].equals(permission.name)){
                                success[i] = true;
                            }
                        }
                        boolean apply = true;
                        for (int i = 0; i < success.length; i++) {
                            if (!success[i]){
                                apply = false;
                                break;
                            }
                        }
                        if (apply){//全部同意
                            listener.isApply();
                        }
                    } else if (permission.shouldShowRequestPermissionRationale) {
                        // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时。还会提示请求权限的对话框
                        DialogUtils.showDialog(compatActivity.getSupportFragmentManager(), R.layout.dialog_exit, 40, false, (holder, dialog) -> {
                            holder.setText(R.id.tvContent, "功能受限，是否重新获取权限？");
                            holder.getView(R.id.tvConfirm).setOnClickListener(v -> {
                                dialog.dismiss();
                                initPermission(compatActivity, requestCode, listener, permissions);
                            });
                            holder.getView(R.id.tvCancel).setOnClickListener(v -> {
                                dialog.dismiss();
                                ActivityManager.getAppManager().AppExit();
                            });
                        });
                    } else {
                        // 用户拒绝了该权限，而且选中『不再询问』
                        DialogUtils.showDialog(compatActivity.getSupportFragmentManager(), R.layout.dialog_exit, 40, false, (holder, dialog) -> {
                            holder.setText(R.id.tvContent, "功能受限，是否重新获取权限？");
                            holder.getView(R.id.tvConfirm).setOnClickListener(v -> {
                                dialog.dismiss();
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", BuildConfig.APPLICATION_ID, null);
                                intent.setData(uri);
                                compatActivity.startActivityForResult(intent, requestCode);
                            });
                            holder.getView(R.id.tvCancel).setOnClickListener(v -> {
                                dialog.dismiss();
                                ActivityManager.getAppManager().AppExit();
                            });
                        });
                    }
                });
    }


    public interface RxPermissionListener{
        void isApply();
    }
}
